package com.tianbo.elfii.client.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;
import com.tianbo.elfii.client.common.wlanmanagement.BaseDialogPane;

public class WlanCustomConnectPane extends BaseDialogPane {

	public JTextField inputField = null;
	public ElfiiCommonKeyboard keyboard = null;
	public ElfiiCommonConfigNetWifi wifiConfig = null;
	public WlanAP connectingAP = null;
	public WlanCustomConnectDialog dialog = null; 
	public static WlanConnectingThread  wlanConnectingThread = null;

	public WlanCustomConnectPane(Object obj) {
		super();
		// TODO Auto-generated constructor stub
		
		wifiConfig = (ElfiiCommonConfigNetWifi)obj;
		
		connectingAP = wifiConfig.selectedApPanel.ap;

		initCustomConnectDialog();
		initKeyboardPane();
	
		dialog.setRelatedKeyboard(keyboard);
		
		dialog.setOpaque(false);
	}
	
	public void initCustomConnectDialog(){
		dialog = new WlanCustomConnectDialog();
		dialog.setBounds(240, 80, 545, 272);
		dialog.init();
		dialog.cancelBtn.addActionListener(new CancelBtnListener());
		dialog.confirmBtn.addActionListener(new ConnectBtnListener());
		this.add(dialog);
	}
	
	

	public void initKeyboardPane() {
		keyboard = new ElfiiCommonLoginKeyboard();
		
		keyboard.setTextField(inputField, 20);

		JPanel keyPanel = keyboard.createKeyPanel();
		keyPanel.setOpaque(false);

		this.add(keyPanel);
		keyPanel.setBounds(150, 380, 735, 300);

		keyboard.createInfoPanel();

	}
	
	public class CancelBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
//			if (wifiConfig.selectedApPanel.ap != null) {
			ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				mainFrame.setContentPane(backPanel);
//				wifiConfig.selectedApPanel.ap = null;
//			}
		}
	}
	
	
	public class ConnectBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (connectingAP != null) {
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				
				if(wlanConnectingThread!=null){
					wlanConnectingThread.cancel();
				}
				connectingAP.setSSID(dialog.ssidInputField.getText().trim());
				
//				if(ElfiiCommonConfigUtil.searchedApList.containsKey(connectingAP.getSSID())){
					for(int i=0;i<wifiConfig.apPanelList.size();i++){
						if(wifiConfig.apPanelList.get(i).ap.getSSID().equals(connectingAP.getSSID())){
							wifiConfig.selectedApPanel = wifiConfig.apPanelList.get(i);
							connectingAP = wifiConfig.apPanelList.get(i).ap;
						}
					}
//				}
				
				connectingAP.setPassword(dialog.pwInputField.getText());
				connectingAP.setEncryptedStat(dialog.isEncryptFlag);
				wlanConnectingThread = new WlanConnectingThread(wifiConfig);
				connectingAP.setCurStat(APSTAT.CONNECTING);
				wlanConnectingThread.start();
				mainFrame.setContentPane(backPanel);
			}
		}
	}
}
